package com.ykq.domain;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.util.StringUtils;

/**
 * 系统用户
 */
@ApiModel(value = "com-ykq-domain-SysUser")
@Data
@AllArgsConstructor
@NoArgsConstructor
@TableName(value = "sys_user")
public class SysLoginUser implements UserDetails {


    @TableId(value = "user_id", type = IdType.AUTO)
    @ApiModelProperty(value = "")
    private Long userId;

    /**
     * 用户名
     */
    @TableField(value = "username")
    @ApiModelProperty(value = "用户名")
    private String username;

    /**
     * 密码
     */
    @TableField(value = "password")
    @ApiModelProperty(value = "密码")
    private String password;


    /**
     * 状态  0：禁用   1：正常
     */
    @TableField(value = "status")
    @ApiModelProperty(value = "状态  0：禁用   1：正常")
    private Integer status;

    /**
     * 用户所在的商城Id
     */
    @TableField(value = "shop_id")
    @ApiModelProperty(value = "用户所在的商城Id")
    private Long shopId;


    @TableField(exist = false)
    @ApiModelProperty(value = "后台管理用户的权限")
    private List<String> auths;

    /**
     * 权限
     *
     * @return
     */
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        ArrayList<SimpleGrantedAuthority> authorities = new ArrayList<>();
        if (!CollectionUtils.isEmpty(auths)) {
            auths.forEach(auth -> {
                // null  ，分割
                if (StringUtils.hasText(auth)) {
                    // 分割
                    String[] split = auth.split(",");
                    for (String s : split) {
                        authorities.add(new SimpleGrantedAuthority(s));
                    }
                }
            });
        }
        return authorities;
    }

    /**
     * 因为用id特别多
     * SecurityContextHolder.getContext().getAuthentication().getPrincipal() 用户名
     *
     * @return
     */
    public String getUsername() {
        return String.valueOf(this.userId);
    }

    /**
     * 密码
     *
     * @return
     */
    public String getPassword() {
        return this.password;
    }


    @Override
    public boolean isAccountNonExpired() {
        return this.status == 1;
    }

    @Override
    public boolean isAccountNonLocked() {
        return this.status == 1;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return this.status == 1;
    }

    @Override
    public boolean isEnabled() {
        return this.status == 1;
    }
}